/**
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nightonke.boommenusample;

import ohos.agp.components.AttrSet;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.VectorElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;
import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

public class ToolBar extends DirectionalLayout {

    private final String tag = "ToolBar";

    private DirectionalLayout leftLayout;
    private Image leftItem;
    private DirectionalLayout centerLayout;
    private Text titleText;
    private Text subTitleText;
    private DirectionalLayout rightLayout;
    private Image searchItem;
    private Image otherItem;
    private Image menuItem;

    private boolean onDrawn = false;

    public ToolBar(Context context) {
        super(context);
        initView(context);
    }

    public ToolBar(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initView(context);
    }

    public ToolBar(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initView(context);
    }

    /**
     * initView
     * @param c
     */
    void initView(Context c) {

        this.setOrientation(HORIZONTAL);
        int p = Utils.vpToPx(c, 10);
        this.setPadding( p, p, p, p);

        this.addComponent(getLeftLayout());
        this.addComponent(getCenterLayout());
        this.addComponent(getRightLayout());

//        ShapeElement element = new ShapeElement();
//        element.setRgbColor(new RgbColor(255, 0, 0));
//        centerLayout.setBackground(element);

        centerLayout.setMarginsLeftAndRight(p,p);
        titleText.setTextSize(20, Text.TextSizeType.FP);

        subTitleText.setTextSize(16, Text.TextSizeType.FP);
        subTitleText.setMarginTop(Utils.vpToPx(c, 5));

        searchItem.setMarginRight(p);
        menuItem.setMarginLeft(p);
        menuItem.setImageElement(new VectorElement(getContext(), ResourceTable.Graphic_ic_ellipsis_vertical_white));

    }

    /**
     * set TitleTextColor
     * @param titleColor
     */
    public void setTitleTextColor(Color titleColor) {
        titleText.setTextColor(titleColor);
    }

    /**
     * set SubTitleTextColor
     * @param titleColor
     */
    public void setSubTitleTextColor(Color titleColor) {
        subTitleText.setTextColor(titleColor);
    }
    public void setTitleTextColor(Color titleColor, Color subTitleColor) {
        setTitleTextColor(titleColor);
        setSubTitleTextColor(subTitleColor);
    }

    /**
     * set Title
     * @param text
     */
    public void setTitle(String text) {
        getTitleView().setText(text);
    }

    /**
     * set SubTitle
     * @param text
     */
    public void setSubTitle(String text) {
        getSubTitleView().setText(text);
    }

    /**
     * set Titles
     * @param text
     * @param subText
     */
    public void setTitles(String text, String subText) {
        setTitle(text);
        setSubTitle(subText);
    }

    /**
     * set LeftImageElement
     * @param element
     */
    public void setLeftImageElement(Element element) {
        getLeftItem().setImageElement(element);
    }

    /**
     * set SearchImageElement
     * @param element
     */
    public void setSearchImageElement(Element element) {
        getSearchItem().setImageElement(element);
    }

    /**
     * set OtherImageElement
     * @param element
     */
    public void setOtherImageElement(Element element) {
        getOtherItem().setImageElement(element);
    }

    /**
     * set MenuVisibility
     * @param visibility
     */
    public void setMenuVisibility(int visibility) {
        getMenuItem().setVisibility(visibility);
    }

    /**
     * set LeftItemOnClickListener
     * @param listener
     */
    public void setLeftItemOnClickListener(ClickedListener listener) {
        getLeftItem().setClickedListener(listener);
    }

    /**
     * set SearchItemOnClickListener
     * @param listener
     */
    public void setSearchItemOnClickListener(ClickedListener listener) {
        getSearchItem().setClickedListener(listener);
    }

    /**
     * set OtherItemOnClickListener
     * @param listener
     */
    public void setOtherItemOnClickListener(ClickedListener listener) {
        getOtherItem().setClickedListener(listener);
    }

    /**
     * set MenuItemOnClickListener
     * @param listener
     */
    public void setMenuItemOnClickListener(ClickedListener listener) {
        getMenuItem().setClickedListener(listener);
    }

    /**
     * get LeftLayout
     * @return
     */
    public DirectionalLayout getLeftLayout() {
        if (leftLayout == null) {
            leftLayout = new DirectionalLayout(getContext());
            leftLayout.setLayoutConfig(new LayoutConfig(MATCH_CONTENT, MATCH_PARENT));
            leftLayout.setOrientation(HORIZONTAL);
            leftLayout.setAlignment(LayoutAlignment.VERTICAL_CENTER);

            leftLayout.addComponent(getLeftItem());
        }
        return leftLayout;
    }

    /**
     * get CenterLayout
     * @return
     */
    public DirectionalLayout getCenterLayout() {
        if (centerLayout == null) {
            centerLayout = new DirectionalLayout(getContext());
            centerLayout.setLayoutConfig(new LayoutConfig(0, MATCH_PARENT, LayoutAlignment.VERTICAL_CENTER, 1));
            centerLayout.setOrientation(VERTICAL);

            centerLayout.addComponent(getTitleView());
            centerLayout.addComponent(getSubTitleView());
        }
        return centerLayout;
    }

    /**
     * get RightLayout
     * @return
     */
    public DirectionalLayout getRightLayout() {
        if (rightLayout == null) {
            rightLayout = new DirectionalLayout(getContext());
            rightLayout.setLayoutConfig(new LayoutConfig(MATCH_CONTENT, MATCH_PARENT));
            rightLayout.setOrientation(HORIZONTAL);
            rightLayout.setAlignment(LayoutAlignment.VERTICAL_CENTER);

            rightLayout.addComponent(getSearchItem());
            rightLayout.addComponent(getOtherItem());
            rightLayout.addComponent(getMenuItem());
        }
        return rightLayout;
    }

    /**
     * get LeftItem
     * @return
     */
    public Image getLeftItem() {
        if (leftItem == null) {
            leftItem = new Image(getContext());
            leftItem.setLayoutConfig(new LayoutConfig(MATCH_CONTENT, MATCH_CONTENT));
        }
        return leftItem;
    }

    /**
     * get TitleView
     * @return
     */
    public Text getTitleView() {
        if (titleText == null) {
            titleText = new Text(getContext());
            titleText.setLayoutConfig(new LayoutConfig(MATCH_PARENT, MATCH_CONTENT, LayoutAlignment.VERTICAL_CENTER, 1));
//            titleText.setTextSize(ResourceTable.Integer_text_size_title);
//            titleText.setTruncationMode(Text.TruncationMode.AUTO_SCROLLING);
//            titleText.setAutoScrollingCount(Text.AUTO_SCROLLING_FOREVER);
//            titleText.startAutoScrolling();
        }
        return titleText;
    }

    /**
     * get SubTitleView
     * @return
     */
    public Text getSubTitleView() {
        if (subTitleText == null) {
            subTitleText = new Text(getContext());
            subTitleText.setLayoutConfig(new LayoutConfig(MATCH_PARENT, MATCH_CONTENT));
//            subTitleText.setTextSize(ResourceTable.Integer_text_size_sub_title);
//            subTitleText.setTruncationMode(Text.TruncationMode.AUTO_SCROLLING);
//            subTitleText.setAutoScrollingCount(Text.AUTO_SCROLLING_FOREVER);
//            subTitleText.startAutoScrolling();
        }
        return subTitleText;
    }

    /**
     * get SearchItem
     * @return
     */
    public Image getSearchItem() {
        if (searchItem == null) {
            searchItem = new Image(getContext());
            searchItem.setLayoutConfig(new LayoutConfig(MATCH_CONTENT, MATCH_CONTENT));
        }
        return searchItem;
    }

    /**
     * get OtherItem
     * @return
     */
    public Image getOtherItem() {
        if (otherItem == null) {
            otherItem = new Image(getContext());
            otherItem.setLayoutConfig(new LayoutConfig(MATCH_CONTENT, MATCH_CONTENT));
        }
        return otherItem;
    }

    /**
     * get MenuItem
     * @return
     */
    public Image getMenuItem() {
        if (menuItem == null) {
            menuItem = new Image(getContext());
            menuItem.setLayoutConfig(new LayoutConfig(MATCH_CONTENT, MATCH_CONTENT));
        }
        return menuItem;
    }

}
